Ext.define('iMedproplus.view.admin.structure.TreePanel', {
    extend: 'Ext.tree.Panel',
    alias: 'widget.adminstructuretreepanel',
    title: 'Struktura Organizacyjna',

    initComponent: function() {
        var me = this;
        var treePanelMenu = Ext.create('Ext.menu.Menu', { 
            id: 'treepanel-menu',
            items: [
            {                                
                xtype: 'menuitem',
                icon: './resources/icons/delete.png',
                text: 'Usuń',
                listeners: {
                    click: {
                        fn: me.onMenuitemClick,
                        scope: me
                    }
                }            
            }
            ]
        });
        Ext.applyIf(me, {
            collapsed: true,
            stateful: false,
            store: 'iMedproplus.store.StructureTree',
            displayField: 'text',
            viewConfig: {
                autoScroll: false,
                disableSelection: false,
                loadingText: '\u0141aduję',
                rootVisible: false
            },
            listeners: {
                selectionchange: {
                    fn: me.onTreepanelSelectionChange,
                    scope: me
                },
                beforeitemcontextmenu: {
                    fn: me.onTreepanelBeforeItemContextMenu,
                    scope: me
                }
            },
            items: [ treePanelMenu ]
        });

        me.callParent(arguments);
    },
    
    onTreepanelSelectionChange: function(tablepanel, selections, options) {
        this.fireEvent('structureNodeSelectionChange', selections);
    },
    
    onTreepanelBeforeItemContextMenu: function(tablepanel, record, item, index, e, options) {
        e.stopEvent();
        var menu = Ext.getCmp('treepanel-menu');
        menu.showAt(e.getXY());
    },
    
    onMenuitemClick: function(item, e, options) {
        this.fireEvent('removeOrganizationStructureFromTree');
    }

});